﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Ymatou.CommonService;

namespace Ymatou.UserAuth.Utility
{
    public class AuthHelp
    {
        public static string ParametersEncoding(string val)
        {
            return string.IsNullOrEmpty(val) ? string.Empty : System.Web.HttpUtility.UrlEncode(val);
        }
        public static string UserEncryptPassowrd(string pas)
        {
            var encrypt = new Encrypt();
            return encrypt.Md5Encrypt(encrypt.AesEncrypt(pas, true));
        }
        public static bool DecryptCookieToken(string userId, string userType, string token)
        {
            var decToken = new Encrypt().AesDecrypt(token, true)
                                        .Split(',');
            if (decToken.Length != 3)
            {
                return false;
            }

            return decToken[0] == userId && decToken[1] == userType;
        }

        /// <summary>
        ///     生成cookie token,用户登录的时候,会验证token ,判断cookie是否被修改
        /// </summary>
        public static string GenerateCookieToken(int userId
                                                , int userType
                                                , string logonTime)
        {
            string strRawToken = userId + "," + userType + "," + logonTime;
            return new Encrypt().AesEncrypt(strRawToken, true);
        }
        public static string GenerateCookieToken(int userId
                                                , int userType)
        {
            string strRawToken = userId + "," + userType + "," + DateTime.Now.ToString("yyyyMMddHHmmss");
            return new Encrypt().AesEncrypt(strRawToken, true);
        }
        public static string GetRequestSourceHost()
        {
            if (HttpContext.Current == null) return string.Empty;
            if (HttpContext.Current.Request.UrlReferrer != null)
                return HttpContext.Current.Request.UrlReferrer.Host;
            return HttpContext.Current.Request.Url.Host;
        }
    }

    public static class TypeConvert
    {
        public static int ToInt32(this string val, int defVal)
        {
            if (string.IsNullOrEmpty(val)) return defVal;
            int _r;
            if (int.TryParse(val, out _r))
            {
                return _r;
            }
            else return defVal;
        }
    }

    public static class AuthAssert
    {
        public static void AssertStringNotNull(string val, string msg)
        {
            if (string.IsNullOrEmpty(val))
                throw new ArgumentNullException(msg);
        }
        public static void AssertInt32GtOrEqual(int val, int comparisonvalue, string msg)
        {
            if (!(val >= comparisonvalue)) throw new ArgumentNullException(msg);
        }
        public static void Assert(Func<bool> fn, string msg)
        {
            if (!fn()) throw new InvalidOperationException(msg);
        }
        public static void Assert(Func<bool> fn, Exception ex)
        {
            if (!fn()) throw ex;
        }
    }
}
